#import "SwitchClass.h"

#import "BlueClass.h"
#import "RedClass.h"

@implementation SwitchClass

@synthesize blueClass;
@synthesize redClass;

// Implementation of method from view class controller. UIViewControler
// This is called on init.
- (void) viewDidLoad
{

    BlueClass *blue = [[BlueClass alloc]initWithNibName:@"BlueWindow" bundle:nil];
    self.blueClass = blue;
    [self.view insertSubview:blue.view atIndex:0];
    [blue release];
    
    [super viewDidLoad];
}

// Method to swap views.
-(IBAction) switchViews: (id)sender
{
    // Find out which view is showing.
    

    // If the red view is == nothing... show the red view.
    if (self.redClass.view.superview==nil)
    {
        // Check if we have an instance of the class already, else allocate with
        // nib (*.xib) file
        if (self.redClass==nil) 
        {
            RedClass *red = [[RedClass alloc]initWithNibName:@"RedWindow" bundle:nil];
            self.redClass = red;
            [red release];
        }
        // Remove the blue view.
        [blueClass.view removeFromSuperview];
        // Add the red view
        [self.view insertSubview:redClass.view atIndex:0];
    }
    // If the red view is on the screen, add the blue view
    else 
    {
        if (self.blueClass==nil)
        {
            BlueClass *blue = [[BlueClass alloc]initWithNibName:@"BlueWindow" bundle:nil];
            self.blueClass = blue;            
            [blue release];
        }
        // Remove the blue view.
        [redClass.view removeFromSuperview];
        // Add the red view
        [self.view insertSubview:blueClass.view atIndex:0];
    }
}

- (void)didReceiveMemoryWarning 
{
    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

    if (self.blueClass.view.superview==nil)
        self.blueClass=nil;
    else {
        self.redClass=nil;
    }   
}

- (void)viewDidUnload 
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc 
{
    [super dealloc];
    [redClass release];
    [blueClass release];
}


@end
